import { resolve } from 'node:path'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import { img2base64OrNetwork, readFileBuffer, uniRoute, uploadFile } from './plugins'
// vite默认会打包出umd和es模块化两种导出方式的文件，以下配置会打包出两份结果：

export default defineConfig({
  plugins: [
    uniRoute({ root: __dirname, pagesDir: './__test__', moduleDir: './__test__/router', pagesPath: './__test__/pages.json' }),
    img2base64OrNetwork({ root: __dirname, uploadImp: upload }),
    vue()
  ],
  base: './',
  build: {
    lib: {
      entry: resolve(__dirname, 'plugins/index.ts'),
      name: 'index',
      // 构建好的文件名（不包括文件后缀）
      fileName: 'index',
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ['node'],
      output: {
        // 在 UMD 构建模式下,全局模式下为这些外部化的依赖提供一个全局变量
        globals: {
          vue: 'vue',
        }
      }
    }
  }
})

function upload(path, absPath): Promise<string> {
  console.log(path, '======', absPath)
  return Promise.resolve('https://pic1.zhimg.com/70/v2-18cfb0c9b67ade1f73be4d0b8bd690f4_1440w.avis?source=172ae18b&biz_tag=Post')
  // return new Promise((resolve, reject) => {
  //   readFileBuffer(path, (file) => {
  //     uploadFile('https://xxxxxxxx', {
  //       file,
  //       headers: {
  //         'Accept': '*/*',
  //         'Host': 'ecology.ynrczx.com',
  //         'Accept-Encoding': 'gzip,deflate,br',
  //         'Authorization': 'Bearer 2cef698078384f60bfd17ad6a72dbfdc',
  //         'User-Agent': `Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1`,
  //       },
  //       formData: { fileUrl: 'test1/test2/' }
  //     }).then(res => {
  //       console.log(res)
  //       resolve(res.url)
  //     }).catch(e => {
  //       console.error('Upload failed:', e)
  //     })
  //   })
  // })
}
